package ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import math.StatisticsFactory;

import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;

import session.Session;
import ui.SettingPanel.MapFollowsZoom;

import actions.Settings;

public class SpeedPanelControler extends JPanel {

	private JCheckBox[] buttons = new JCheckBox[]{};
	
	private final Session session;
	
	private JComponent radioButtonPanels;

	public SpeedPanelControler(final Session session) {
		this.session = session;
		session.getStatisticsFactory();
		GridLayout layout = new GridLayout(0, 1);
		layout.setVgap(1);
		radioButtonPanels =  Box.createVerticalBox();
		setLayout(new BorderLayout());
		ScrollPane sp = new ScrollPane();
		sp.add(radioButtonPanels);
		sp.setPreferredSize(new Dimension(150, -1));
		
		Box north = Box.createHorizontalBox();
		JButton removeAllB = new JButton("Hide all");
		removeAllB.addActionListener(new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (buttons != null) {
					for (JCheckBox b : buttons) {
						b.setSelected(false);
					}
				}
			}
		});
		north.add(removeAllB);
		north.add(Box.createHorizontalGlue());
		
		final JRadioButton mapFollowsZoomButton = new JRadioButton("Follow Zoom");
		mapFollowsZoomButton.setToolTipText("Map follows the data shown in the graph panel.");
		mapFollowsZoomButton.setSelected(true);
		mapFollowsZoomButton.addActionListener(new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				session.setMapFollowsZoom(mapFollowsZoomButton.isSelected());
			}
		});
		session.setMapFollowsZoom(mapFollowsZoomButton.isSelected());
		north.add(mapFollowsZoomButton);
		
		add(north, BorderLayout.NORTH);
		add(sp, BorderLayout.CENTER);
	}

	public void createActions(SpeedPanel speedPanel) {
		radioButtonPanels.removeAll();
		createButtons(speedPanel);
	}

	public StatisticsFactory getStatisticsFactory() {
		return session.getStatisticsFactory(); 
	}
	
	private void createButtons(SpeedPanel speedPanel) {
		TimeSeriesCollection dataSets = speedPanel.getDataset();
		if (dataSets == null) {
			return;
		}
		List series = dataSets.getSeries();
		buttons = new JCheckBox[series.size()];
		int c = 0;
		String oldGroupName = "";
		for (Object object : series) {
			TimeSeries s = (TimeSeries) object;
			TimeSeriesName id = (TimeSeriesName) s.getKey();
			String name = id.getName();//string.substring(0, 12) + "..." + string.substring(string.length() - 12);
			if (id.getGroupName().equals(oldGroupName) == false) {
				oldGroupName = id.getGroupName();
				JLabel label = new JLabel("File [" + oldGroupName + "]", JLabel.LEFT);
				radioButtonPanels.add(label);
			} 
			JCheckBox rb = new JCheckBox();
			AddorRemoveSeries a = new AddorRemoveSeries(name, s, speedPanel, rb);
			rb.addItemListener(a);
			rb.addActionListener(a);
			rb.setSelected(true);
			rb.setText(name);
			radioButtonPanels.add(rb);
			buttons[c++] = rb;
		
		}
	}
	
	class AddorRemoveSeries extends AbstractAction implements ItemListener {

		private final TimeSeries series;
		private final SpeedPanel speedPanel;
		private final JCheckBox button;

		public AddorRemoveSeries(String name, TimeSeries s, SpeedPanel speedPanel, JCheckBox button) {
			super(name);
			this.series = s;
			this.speedPanel = speedPanel;
			this.button = button;
		}
		
		@Override
		public boolean isEnabled() {
			return true;
		}
		
		@Override
		public void itemStateChanged(ItemEvent e) {
			actionPerformed(null);
		}
		

		@Override
		public void actionPerformed(ActionEvent e) {
			int num = speedPanel.getDataset().indexOf(series);
			if (num >= 0) {
				if (!button.isSelected()) {
					speedPanel.getDataset().removeSeries(num);
				}
			} else {
				if (button.isSelected()) {
					speedPanel.getDataset().addSeries(series);
				}
			}
		}

	}
}
